package controller;

import java.util.List;
import javax.servlet.jsp.JspWriter;
import util.Constants;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import model.Severity;
import session.SeverityFacadeRemote;

/**
 *
 * @author HuyNVK
 */
public class severityController {

    /**
     * Get Action Link HTML code to insert into JSP file.
     */
    public static String getActionLink(String id) {
        String edit = "<a onclick=\"doAction(frmIndex, '" + Constants.ADMIN_PAGE_NAME
                + "', '" + Constants.SEVERITY_PAGE_NAME + "', '" + Constants.SEVERITY_EDIT
                + "', '" + Constants.SHOW_EDIT_PAGE + "', '" + id + "')\">Edit</a>";
        String delete = "<a onclick=\"doAction(frmIndex, '" + Constants.ADMIN_PAGE_NAME
                + "', '" + Constants.SEVERITY_PAGE_NAME + "', '" + Constants.SEVERITY_LIST
                + "', '" + Constants.DELETE + "', '" + id + "')\">Delete</a>";
        return edit + delete;
    }

    /**
     * Show all severities on screen.
     */
    public static void showAllSeverities(JspWriter out) {
        Controller.log.info("showAllSeverities");
        try {
            List<model.Severity> severityList = util.Facade.getSeverityFacade().findAll();

            /*
             * Show Severity List on Screen.
             */
            //Create the title
            out.println("<div id='list-table'>");
            out.println("<div class='row'>");
            out.println("<div class='column-header' style='width: "
                    + Constants.ID_COLUMN_SIZE + ";'>ID</div>");
            out.println("<div class='column-header' style='width: "
                    + Constants.OBJECT_NAME_COLUMN_SIZE + ";'>Name</div>");
            out.println("<div class='column-header' style='width: "
                    + Constants.OBJECT_DESCRIPTION_COLUMN_SIZE + ";'>Description</div>");
            out.println("<div class='column-header' style='width: "
                    + Constants.ACTION_COLUMN_SIZE + ";'>Action</div>");
            out.println("<div class='clear'></div>");
            out.println("</div>");

            //Show Values
            for (int i = 0; i < severityList.size(); i++) {
                Severity node = severityList.get(i);
                out.println("<div class='row'>");
                out.println("<div class='column' style='width: "
                        + Constants.ID_COLUMN_SIZE + ";'>"
                        + node.getSeverityID() + "</div>");
                out.println("<div class='column' style='width: "
                        + Constants.OBJECT_NAME_COLUMN_SIZE + ";'>"
                        + node.getName() + "</div>");
                out.println("<div class='column' style='width: "
                        + Constants.OBJECT_DESCRIPTION_COLUMN_SIZE + ";'>"
                        + node.getDescription() + "</div>");
                out.println("<div class='column' style='width: "
                        + Constants.ACTION_COLUMN_SIZE + ";'>"
                        + getActionLink(node.getSeverityID()) + "</div>");
                out.println("</div>");
                out.println("<div class='clear'></div>");
            }
            out.println("</div>");

            Controller.log.info("showAllSeverities finish.");
        } catch (Exception ex) {
            Controller.log.error("Error showAllSeverities: " + ex.getMessage());
        }
    }

    /**
     * Show the page Edit Severity on the screen.
     */
    public static void showEditSeverityPage(JspWriter out, String id) {
    }

    /**
     * Process action with Severity Page.
     */
    public static void processAction(HttpServletRequest request) {
        String action = request.getParameter("action");
        String selectedID = request.getParameter("selectedItemID");

        Controller.log.info("severityController.processAction");

        try {
            if (Constants.DELETE.equalsIgnoreCase(action)) {
                //Process DELETE action.
                if ((selectedID == null) || "".equals(selectedID)) {
                    //If the selectedID is null, throw an Exception.
                    throw new Exception("No selected id to delete.");
                } else {
                    //Delete selected Severity.
                    util.Facade.getSeverityFacade().deleteByID(selectedID);

                    Controller.sendMessage(request, "Deleted Severity: " + selectedID, false);
                    Controller.log.info("Deleted Severity: " + selectedID);
                }

            } else if (Constants.SHOW_EDIT_PAGE.equalsIgnoreCase(action)) {
                //Process SHOW_EDIT_PAGE action.
                if ((selectedID == null) || "".equals(selectedID)) {
                    //If the selectedID is null, throw an Exception.
                    throw new Exception("No selected id to edit.");
                } else {
                    //Get value to send back to Edit page.
                    Severity severity = util.Facade.getSeverityFacade().find(selectedID);
                    if (severity == null) {
                        throw new Exception("The selected Severity not found.");
                    } else {
                        HttpSession session = request.getSession();
                        session.setAttribute("severityID", severity.getSeverityID());
                        session.setAttribute("name", severity.getName());
                        session.setAttribute("description", severity.getDescription());
                    }
                }

            } else if (Constants.INSERT.equalsIgnoreCase(action)) {
                //Process INSERT action.
                String severityID = request.getParameter("severityID");
                String name = request.getParameter("name");
                String description = request.getParameter("description");

                name = util.Unicode.changeToUnicode(name);
                description = util.Unicode.changeToUnicode(description);

                if ((severityID == null) || "".equals(severityID)) {
                    //If the ID is null.
                    Controller.sendMessage(request, "Please input ID.", true);
                    throw new Exception("The input ID is null.");
                } else if ((name == null) || "".equals(name)) {
                    //If the Name is null.
                    Controller.sendMessage(request, "Please input Name.", true);
                    throw new Exception("The input Name is null.");
                } else {
                    SeverityFacadeRemote severityBean = util.Facade.getSeverityFacade();
                    if (severityBean.find(severityID) != null) {
                        //If the severity id already exists.
                        Controller.sendMessage(request, "The Severity ID have already exists,", true);
                        throw new Exception("The Severity ID have already exists.");
                    } else {
                        //Insert new severity into DB.
                        Severity temp = new Severity(severityID, name);
                        temp.setDescription(description);

                        severityBean.insert(temp);
                    }
                }

                Controller.sendMessage(request, "Insert Successfully.", false);
                Controller.log.info("Insert Severity: " + severityID);

            } else if (Constants.EDIT.equalsIgnoreCase(action)) {
                //Process EDIT action.
                String severityID = request.getParameter("severityID");
                String name = request.getParameter("name");
                String description = request.getParameter("description");

                name = util.Unicode.changeToUnicode(name);
                description = util.Unicode.changeToUnicode(description);

                if ((name == null) || "".equals(name)) {
                    //If the Name is null.
                    Controller.sendMessage(request, "Please input Name.", true);
                    throw new Exception("The input Name is null.");
                } else {
                    SeverityFacadeRemote severityBean = util.Facade.getSeverityFacade();
                    Severity severity = severityBean.find(severityID);
                    if (severity == null) {
                        throw new Exception("Cannot found the target Severity.");
                    } else {
                        severity.setName(name);
                        severity.setDescription(description);
                        severityBean.edit(severity);
                    }
                }

                Controller.sendMessage(request, "Save Successfully.", false);
                Controller.log.info("Save Severity: " + severityID);
            }
        } catch (Exception ex) {
            Controller.log.error("Error severityController.processAction" + ex.getMessage());
        }
    }
}
